<template>
  <div class="app-container">
    <el-table
      ref="dragTable"
      v-loading="listLoading"
      :data="list"
      row-key="id"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column align="center" label="ID" width="65">
        <template #default="{ row }">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column width="180px" align="center" label="Date">
        <template #default="{row}">
          <span>{{ parseTime(row.timestamp, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="300px" label="Title">
        <template #default="{row}">
          <span>{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column width="110px" align="center" label="Author">
        <template #default="{ row }">
          <span>{{ row.author }}</span>
        </template>
      </el-table-column>
      <el-table-column width="100px" label="Importance">
        <template #default="{row}">
          <svg-icon v-for="(n, index) in new Array(row.importance)" :key="index" icon-class="star" class="icon-star" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="Readings" width="95">
        <template #default="{row}">
          <span>{{ row.pageviews }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="Readings" width="95">
        <template #default="{row}">
          <span>{{ row.pageviews }}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="Status" width="110">
        <template #default="{row}">
          <el-tag :type="statusFilter(row.status)">
            {{ row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="Drag" width="80">
        <template #default>
          <svg-icon icon-class="drag" class="drag-handler" />
        </template>
      </el-table-column>
    </el-table>
    <div class="show-d">
      <el-tag>The default order :</el-tag>
      {{ oldList }}
    </div>
    <div class="show-d">
      <el-tag>The after dragging order :</el-tag>
      {{ newList }}
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, nextTick } from 'vue'
import { fetchList } from '@/api/article'
import Sortable from 'sortablejs'
import { parseTime } from '@/utils'
import SvgIcon from '@/components/SvgIcon'

const statusFilter = status => {
  const statusMap = {
    published: 'success',
    draft: 'info',
    deleted: 'danger'
  }

  return statusMap[status]
}

const list = ref(null)
const total = ref(null)
const listLoading = ref(true)
const listQuery = reactive({
  page: 1,
  limit: 10
})
const sortable = ref(null)
const oldList = ref([])
const newList = ref([])
const dragTable = ref()

const getList = async() => {
  listLoading.value = true
  const { data } = await fetchList(listQuery)
  list.value = data.items
  total.value = data.total
  listLoading.value = false
  oldList.value = list.value.map(v => v.id)
  newList.value = oldList.value.slice()
  nextTick(() => {
    setSort()
  })
}

const setSort = () => {
  const el = dragTable.value.$el.querySelectorAll('.el-table__body-wrapper .el-table__body > tbody')[0]
  sortable.value = Sortable.create(el, {
    ghostClass: 'sortable-ghost', // Class name for the drop placeholder
    setData: function(dataTransfer) {
      // to avoid firefox bug
      // Detail see: https://github.com/RubeXa/Sortable/issues/1012
      dataTransfer.setData('Text', '')
    },
    onEnd: evt => {
      const targetRow = list.value.splice(evt.oldIndex, 1)[0]
      list.value.splice(evt.newIndex, 0, targetRow)

      // for show the changes, you can delete in you code
      const tempIndex = newList.value.splice(evt.oldIndex, 1)[0]
      newList.value.splice(evt.newIndex, 0, tempIndex)
    }
  })
}

getList()
</script>

<script>
export default {
  name: 'DragTable'
}
</script>

<style>
.sortable-ghost {
  opacity: .8;
  color: #fff !important;
  background: #42b983 !important;
}
</style>

<style scoped>
.icon-star {
  margin-right: 2px;
}

.drag-handler {
  width: 20px;
  height: 20px;
  cursor: pointer;
}

.show-d {
  margin-top: 15px;
}
</style>
